<?php

include_once '..\clases\ConexionBD.php';
include_once '..\utils\ConstantesGlobales.php';

session_start();
if(filter_input(INPUT_POST,'accion') == "buscar"){
    
    $conexionBD = unserialize($_SESSION['conexionBD']); /*@var $conexionBD ConexionBD*/
    $fecha = filter_input(INPUT_POST,'fechasHTML');
    $fecha = filter_input(INPUT_POST,'fecha');
    $docBus = $_SESSION['busdocumento'];
    $query = $GLOBALS['consultaClienteDocumentoTodo'] . "" . $docBus;
    $conexionBD->abrirConexion();
    $resultTodo = mysql_query($query,$conexionBD->getConexion());
    $conexionBD->cerrarConexion();
    
    while ($row = mysql_fetch_array($resultTodo, MYSQL_ASSOC)){
        $_SESSION['tipoDocPaciente'] = $row['TIPONIT'];
        $_SESSION['documentoPaciente'] = $row['NIT'];   
        $_SESSION['nombresPaciente'] = $row['NOMBRES'];
        $_SESSION['apellidoPacientes'] = $row['APELLIDOS'];
        $_SESSION['fechaNacimientoPaciente'] = $row['FECHANAC'];
        $_SESSION['edad'] = $row['EDAD'];
        $_SESSION['sexoPaciente'] = $row['SEXO'];
        $_SESSION['lugarNacimientoPaciente'] = remplazarTildez($row['LUGARNAC']);
        $_SESSION['estadoCivilPaciente'] = $row['ESTCIVIL'];
        $_SESSION['direccionPaciente'] = $row['DIRBARRIO'];
        $_SESSION['telResPaciente'] = $row['TELRESD'];
        $_SESSION['celularPaciente'] = $row['CELULAR'];
        $_SESSION['ocupacionPaciente'] = $row['OCUPACION'];
        $_SESSION['telOficinaPaciente'] = $row['TELOFICINA'];
        $_SESSION['emailPaciente'] = $row['EMAIL'];
        $_SESSION['epsPaciente'] = $row['EPS'];
        $_SESSION['vinculacionPaciente'] = $row['VINC'];
     }
     $_SESSION['nombrePacienteCompleto'] = $_SESSION['nombresPaciente'] ." " . $_SESSION['apellidoPacientes'];
     
     $query = "SELECT FAMILIARES, PERSONALES, QUIRURGICOS FROM antecedentes WHERE NITPACIENTE = '$docBus' AND 
        FECHACONS = (SELECT MAX(FECHACONS) FROM antecedentes WHERE NITPACIENTE = '$docBus')";
    $conexionBD->abrirConexion();
    $resultTodo = mysql_query($query,$conexionBD->getConexion());
    $conexionBD->cerrarConexion();
    
    while ($row = mysql_fetch_array($resultTodo, MYSQL_ASSOC)){    
        $_SESSION['antecedenteFamiliares'] = $row['FAMILIARES'];
        $_SESSION['antecedentePersonales'] = $row['PERSONALES'];
        $_SESSION['antecedentesQuirurgicos'] = $row['QUIRURGICOS'];
    }

    //Acompañante y Acudiente
    $query = "SELECT NOMBREACU, TELACU, PARENTACU, NOMBREACO, TELACO, PARENTACO
    FROM paciente pac
    JOIN acudyacomp acu ON pac.nit = acu.nitpaciente
    WHERE pac.nit =  '$docBus'
    AND acu.fechacons =  '$fecha'";
    
    $conexionBD->abrirConexion();
    $resultTodo = mysql_query($query,$conexionBD->getConexion());
    $conexionBD->cerrarConexion();
    
     while ($row = mysql_fetch_array($resultTodo, MYSQL_ASSOC)){    
          $_SESSION['datosAcudiente'] = "Nombre: ".$row['NOMBREACU']." Tel:".$row['TELACU']." Parentesco:".$row['PARENTACU'];
          $_SESSION['datosAcmponante'] = "Nombre: ".$row['NOMBREACO']." Tel:".$row['TELACO']." Parentesco:".$row['PARENTACO'];
     }
     
     //Diagnostico
     $query = "SELECT * FROM paciente pac JOIN diagnostico diag  ON pac.nit = diag.nitpaciente WHERE pac.nit = '$docBus' AND diag.fechacons = '$fecha'";
     
     $conexionBD->abrirConexion();
     $resultTodo = mysql_query($query,$conexionBD->getConexion());
     $conexionBD->cerrarConexion();

     while ($row = mysql_fetch_array($resultTodo, MYSQL_ASSOC)){ 
          $_SESSION['diagnostico'] = $row['DIAGDESC'];
          $_SESSION['cie10'] = $row['CIE10'];
          $_SESSION['tipoDiag'] = $row['TIPODIAG'];
     }

     
     //Dinamica Patologico
     $query = "SELECT * FROM paciente pac JOIN dinapato dinpat  ON pac.nit = dinpat.nitpaciente WHERE pac.nit = '$docBus' AND dinpat.fechacons = '$fecha'";
     
     $conexionBD->abrirConexion();
     $resultTodo = mysql_query($query,$conexionBD->getConexion());
     $conexionBD->cerrarConexion();

     while ($row = mysql_fetch_array($resultTodo, MYSQL_ASSOC)){ 
          $_SESSION['dinaPato'] = $row['DINAMPATOLO'];
     }
     
     //Examen Complementario
     $query = "SELECT * FROM paciente pac JOIN examcomp excp  ON pac.nit = excp.nitpaciente WHERE pac.nit = '$docBus' AND excp.fechacons = '$fecha'";
     
     $conexionBD->abrirConexion();
     $resultTodo = mysql_query($query,$conexionBD->getConexion());
     $conexionBD->cerrarConexion();

     while ($row = mysql_fetch_array($resultTodo, MYSQL_ASSOC)){ 
         $_SESSION['examnComp'] = $row['EXAMPDESC'];
     }
     
     
     //Fisico
     $query = "SELECT * FROM paciente pac JOIN fisico fsc  ON pac.nit = fsc.nitpaciente WHERE pac.nit = '$docBus' AND fsc.fechacons = '$fecha'";
     
     $conexionBD->abrirConexion();
     $resultTodo = mysql_query($query,$conexionBD->getConexion());
     $conexionBD->cerrarConexion();

     while ($row = mysql_fetch_array($resultTodo, MYSQL_ASSOC)){ 
           $_SESSION['presArt'] = $row['SISTOLE']."/".$row['DIASTOLE'];
           $_SESSION['frecCard'] = $row['FCARDIACA'];
           $_SESSION['frecResp'] = $row['FRESPIRA'];
           $_SESSION['peso'] = $row['PESO'];
           $_SESSION['talla'] = $row['TALLA'];
           $_SESSION['temp'] = $row['TEMPERATURA'];
     }
     
     //Fisico General
     $query = "SELECT * FROM paciente pac JOIN `general` gen  ON pac.nit = gen.nitpaciente WHERE pac.nit = '$docBus' AND gen.fechacons = '$fecha'";
     
     $conexionBD->abrirConexion();
     $resultTodo = mysql_query($query,$conexionBD->getConexion());
     $conexionBD->cerrarConexion();

     while ($row = mysql_fetch_array($resultTodo, MYSQL_ASSOC)){ 
        $_SESSION['cabeza'] = $row['CABEZA'];
        $_SESSION['cuello'] = $row['CUELLO'];
        $_SESSION['torax'] = $row['TORAX'];
        $_SESSION['abdomen'] = $row['ABDOMEN'];
        $_SESSION['pelvis'] = $row['PELVIS'];
        $_SESSION['extremidades'] = $row['EXTREMIDADES'];
     }
     
     //Motivo
     $query = "SELECT * FROM paciente pac JOIN motivo mt  ON pac.nit = mt.nitpaciente WHERE pac.nit = '$docBus' AND mt.fechacons = '$fecha'";
     
     $conexionBD->abrirConexion();
     $resultTodo = mysql_query($query,$conexionBD->getConexion());
     $conexionBD->cerrarConexion();

     while ($row = mysql_fetch_array($resultTodo, MYSQL_ASSOC)){ 
         $_SESSION['motivoConsulta'] = $row['MOTYENF'];
     }
     
     //Revsion por sistemas
     $query = "SELECT * FROM paciente pac JOIN revsistemas sis  ON pac.nit = sis.nitpaciente WHERE pac.nit = '$docBus' AND sis.fechacons = '$fecha'";
     
     $conexionBD->abrirConexion();
     $resultTodo = mysql_query($query,$conexionBD->getConexion());
     $conexionBD->cerrarConexion();

     while ($row = mysql_fetch_array($resultTodo, MYSQL_ASSOC)){ 
            $_SESSION['sistDig'] = $row['DIGESTIVO'];
            $_SESSION['sisCard'] = $row['CARDIOVASCULAR'];
            $_SESSION['sistResp'] = $row['RESPIRATORIO'];
            $_SESSION['sistGeni'] = $row['GENITOURINARIO'];
            $_SESSION['sistOsteo'] = $row['OSTEOMUSCULAR'];
            $_SESSION['sistEndo'] = $row['ENDOCRINO'];
            $_SESSION['sistNerv'] = $row['NERVIOSO'];
            $_SESSION['sisPsico'] = $row['PSICOSOMATICO'];
     }
     
     //Tratamiento
     $query = "SELECT * FROM paciente pac JOIN tratamiento trt  ON pac.nit = trt.nitpaciente WHERE pac.nit = '$docBus' AND trt.fechacons = '$fecha'";
     
     $conexionBD->abrirConexion();
     $resultTodo = mysql_query($query,$conexionBD->getConexion());
     $conexionBD->cerrarConexion();

     while ($row = mysql_fetch_array($resultTodo, MYSQL_ASSOC)){ 
         $_SESSION['tratamiento'] = $row['TRATADESC'];
     }
    
    
    session_write_close();
    header('Location: ../formularios/FormularioConsulta.php');
    exit();
    
}else
{
    header('Location: ../principal.php');
    exit();
}

function remplazarTildez($mensaje){
    
    $aacento = utf8_decode('á');
    $eacento = utf8_decode('é');
    $iacento = utf8_decode('í');
    $oacento = utf8_decode('ó');
    $uacento = utf8_decode('ú');

    $Aacento = utf8_decode('Á');
    $Eacento = utf8_decode('É');
    $Iacento = utf8_decode('Í');
    $Oacento = utf8_decode('Ó');
    $Uacento = utf8_decode('Ú');
    
    $contilde = array('á','é','í','ó','ú','Á','É','Í','Ó','Ú');
    $conutf = array($aacento,$eacento,$iacento,$oacento,$uacento,$Aacento,$Eacento,$Iacento,$Oacento,$Uacento);
    
    return str_replace($contilde,$conutf,$mensaje);
    
}

?>
